Ext.namespace("Nippy.Components.NodeEditors");

Nippy.Components.NodeEditors.NodeEditor = Ext.extend(Nippy.Components.NodeEditors.BasicEditor, {
    constructor: function(config){
		Nippy.Components.NodeEditors.NodeEditor.superclass.constructor.apply(this, arguments);
    }
	,initComponent: function(){
		if (null!=this.nodeId)
		{
			Ext.Ajax.request({
				url: '/admin/tree-node/?id='+this.nodeId,
				scope: this,
				failure: function(rsp){Ext.Msg.alert("Ошибка","Не удалось загрузить узел: "+rsp);},
				success: this.nodeLoaded
			});
		}
		
		
		Ext.apply(this, {
		    labelWidth: 135, // label settings here cascade unless overridden
		    frame:true,
		    title: 'Узел: '.concat((this.nodeId!=null)? this.nodeId : 'новый'),
		    bodyStyle:'padding:5px 5px 0',
		    width: 550,
		    defaultType: 'textfield',
		    items: [{
		            fieldLabel: 'Внутреннее название',
		            name: 'Name',
		            allowBlank:false
		            /*test*/
		            /*,listeners: {
					    	valid: function(f){
			    				var v = f.getValue();
			    				var f = f.ownerCt.getForm();
			    				f.setValues(
			    					{
			    						CfgController: 8
			    						,URL: 'empty'
			    						,Title: v
			    						,MetaKeywords: v
			    						,MetaDescription: v
			    						,MenuTitle: v
			    					}
			    				);
			    		}
		    		}*/
	        	}
	        	, new Ext.form.ComboBox({
	        		typeAhead: true,
	            	fieldLabel: 'Контроллер',
	            	name: 'CfgController',
	            	store: this.cfgStore,
	            	displayField: 'Name',
	            	valueField: 'Id',
	            	hiddenName: 'CfgId',
	            	allowBlank: false,
	            	forceSelection: true,
	            	//mode: 'remote',
	            	mode: 'local',
	            	selectOnFocus: true,
	            	triggerAction: 'all',
	            	emptyText: 'Выберите конфигурацию',
	            	valueNotFoundText: 'Выберите конфигурацию'
	            })
	        	, new Ext.form.ComboBox({
	        		typeAhead: true,
	            	fieldLabel: 'Шаблон',
	            	name: 'Layout',
	            	store: this.layoutStore,
	            	displayField: 'Name',
	            	valueField: 'Id',
	            	hiddenName: 'LayoutId',
	            	allowBlank: false,
	            	forceSelection: true,
	            	//mode: 'remote',
	            	mode: 'local',
	            	selectOnFocus: true,
	            	triggerAction: 'all',
	            	emptyText: 'Выберите шаблон',
	            	valueNotFoundText: 'Выберите шаблон'
	            })
		    	,{
		            fieldLabel: 'URL',
		            name: 'URL',
		            allowBlank:false
		        }
		        ,{
		            fieldLabel: 'Title',
		            name: 'Title',
		            allowBlank: false
		        }
		        ,{
		            fieldLabel: 'META Keywords',
		            name: 'MetaKeywords',
		            allowBlank: false
		        }
		        ,{
		            fieldLabel: 'META Description',
		            name: 'MetaDescription',
		            allowBlank: false
		        }
		        ,{
		            fieldLabel: 'Публиковать в меню',
		            name: 'InMenu',
		            xtype: 'checkbox',
		            allowBlank: false,
		            inputValue: 1
		        }
		        ,{
		            fieldLabel: 'Название для меню',
		            name: 'MenuTitle',
		            allowBlank: true
		        }
		    ]
		});
		Nippy.Components.NodeEditors.NodeEditor.superclass.initComponent.apply(this, arguments);
		//this.buttons[0].handler = this.onsave;
	}
	,nodeLoaded: function(rsp){
		var r = eval("("+rsp.responseText+")");
		this.fillForm(r);
	}
	,saveClick: function(){
		var p; var m;
		if (null!=this.nodeId)
		{
			m = 'PUT';
			p = {id: this.nodeId};
		}
		else{
			m = 'POST';
			p = {pId: this.parentId};
		}
		
		this.getForm().submit({
			url: '/admin/tree-node/',
			method: m,
			scope: this,
			params: p,
			failure: this.submitFailure,
			success: function(form, action){
				pId = -1;
				if (null!=this.parentId)
				{
					pId = this.parentId;
					this.parentId = null;
					this.nodeId = action.result.id;
					this.setTitle('Узел: '.concat(this.nodeId));
				}				
				this.fireEvent('onsaved', this);
			}
		});
	}
});